package com.byinsight.web.actions.util;


public class TimeDimensionDefinition {

	
	public static String getXml() {
//		try {
//		InputStream is = TimeDimensionDefinition.class.getResourceAsStream("timedimdef.xml");
//		String ret = convertStreamToString(is);
//		ret = new String(ret.getBytes("ISO8859-1"), "utf-8");
//		return ret;
//		} catch (Exception e) {
//			e.printStackTrace();
//			return "";
//		}
		return timeDimension();
	}
	
	public static String timeDimension() {
		String t = "<dimension name=\"时间\" id=\"date\"><node id=\"date\" name=\"时间\">";
		t += year();
		t += "</node></dimension>";
		return t;
	}
	
	public static String year() {
		String y = "<node id=\"TIME2009\" name=\"2009\">";
		y += "\n";
		for (int i = 0; i < 12; i++) {
			y += month(i);
		}
		y += "</node>";
		return y;
	}
	
	public static String month(int month) {
		String m = "<node id=\"TIME2009-" + format(month) + "\" name=\"" + (month+1) + "月\">";
		m += "\n";
		for (int i = 1; i < 5; i++) {
			m += week(month, i);
		}
		m += "</node>";
		m += "\n";
		return m;
	}
	
	public static String week(int month, int week) {
		String w = "<node id=\"TIME2009-" + format(month) + "-" + format(week) + "\" name=\"" + week + "周\"/>";
		w += "\n";
		return w;
	}
	
	public static String format(int i) {
		return i>=10 ? ""+i : "0"+i;
	}
	
//	public static String convertStreamToString(InputStream is) {
//        /*
//         * To convert the InputStream to String we use the BufferedReader.readLine()
//         * method. We iterate until the BufferedReader return null which means
//         * there's no more data to read. Each line will appended to a StringBuilder
//         * and returned as String.
//         */
//        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
//        StringBuilder sb = new StringBuilder();
// 
//        String line = null;
//        try {
//            while ((line = reader.readLine()) != null) {
//                sb.append(line + "\n");
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//        } finally {
//            try {
//                is.close();
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
// 
//        return sb.toString();
//    }
	
	
	public static void main(String[] args) {
		String s= getXml();
		System.out.println(s);
	}
}
